//
//  USBMusicObject.m
//  wAudioShare
//
//  Created by 赵帅 on 14-1-10.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "USBMusicObject.h"

@interface USBMusicObject()
<serviceGetUSBMusicInfoDelegate>
{
    int musicIndex;
}

@end

@implementation USBMusicObject

-(id)initWithIndex:(int)index
{
    if(self = [super init])
    {
        musicIndex = index;
    }
    return self;
}

-(NSString *)getMusicTitle
{
    NSString * fileName = nil;
    if([_infoDic[@"Title"] length] == 0)
    {
        fileName = [_infoDic objectForKey:@"file"];
        NSArray * array = [fileName componentsSeparatedByString:@"/"];
        fileName = array[[array count]-1];
    }
    else
    {
        fileName = _infoDic[@"Title"];
    }
    
    return fileName;
}

-(void)updateInfo
{
    if(_infoDic[@"Title"] == nil)
    {
        [[GlobalService sharedInstance] getUSBMusicInfo:self fileIndex:musicIndex];
    }
}

-(NSString *)decode:(NSString*)hexStr
{
    if([hexStr length] == 0 || ([hexStr length])%2 != 0 )
    {
        return nil;
    }
    
    int byteLength = [hexStr length] /2;
    char * bytes = malloc(byteLength);
    
    
    int temp=0;
    for(int i=0;i<byteLength;i++)
    {
        temp = [self hex2Dec:[hexStr characterAtIndex:2*i]*16+[hexStr characterAtIndex:2*i+1]];
        temp = [self hex2Dec:[hexStr characterAtIndex:2*i]]*16+[self hex2Dec:[hexStr characterAtIndex:2*i+1]];
        bytes[i]=(char)( temp<128 ? temp : temp-256 ) ;
    }
    
    NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSString*pageSource = [[NSString alloc] initWithBytes:bytes length:byteLength encoding:gbkEncoding];
    
    return pageSource;
}

-(int)hex2Dec:(char)ch
{
	if(ch == '0') return 0;
	if(ch == '1') return 1;
	if(ch == '2') return 2;
	if(ch == '3') return 3;
	if(ch == '4') return 4;
	if(ch == '5') return 5;
	if(ch == '6') return 6;
	if(ch == '7') return 7;
	if(ch == '8') return 8;
	if(ch == '9') return 9;
	if(ch == 'a') return 10;
	if(ch == 'A') return 10;
	if(ch == 'B') return 11;
	if(ch == 'b') return 11;
	if(ch == 'C') return 12;
	if(ch == 'c') return 12;
	if(ch == 'D') return 13;
	if(ch == 'd') return 13;
	if(ch == 'E') return 14;
	if(ch == 'e') return 14;
	if(ch == 'F') return 15;
	if(ch == 'f') return 15;
	else return -1;
}

-(void)serviceGetUSBMusicInfoCallBack:(NSDictionary*)dicCallBack
{
    NSDictionary * dic = dicCallBack;
    
    if(dic == nil || [[dic objectForKey:@"responsestatus"] intValue] == [SERVICE_FAIL intValue])
    {
        
        return;
    }
    else if([[dic objectForKey:@"responsestatus"] intValue] == [SERVICE_CONNECTIONFAIL intValue] || [[dic objectForKey:@"responsestatus"] intValue] == [SERVICE_TIMEOUT intValue])
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PopTabBarController" object:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ErrorComeBack" object:nil];
    }
    
    if(_infoDic == nil) _infoDic = [[NSMutableDictionary alloc] init];
    [_infoDic addEntriesFromDictionary:dicCallBack];
    
    if([_infoDic[@"Title"] length] != 0)
    {
        NSString * str = [self decode:[dicCallBack[@"Title"] uppercaseString]];
        [_infoDic setObject:str forKey:@"Title"];
    }
}

@end
